package com.angel.controller;

import com.angel.bean.ResourceCombination;
import com.angel.bean.VipInfo;
import com.angel.bean.dto.VipInfoDto;
import com.angel.service.ResourceCombinationService;
import com.angel.service.VipInfoService;
import com.angel.shiro.UserAuthorizingRealm;
import com.angel.utils.UserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户学习中心
 */
@Controller
@RequestMapping("/s")
public class UserStudyControll {
    @Resource
    private ResourceCombinationService resourceCombinationService;

    @Resource
    private VipInfoService vipInfoService;

    @RequestMapping("/study")
    public String learnexp(@RequestParam(value = "posion", defaultValue = "1") String posion, Model model){
        //System.out.print("---------显示学习列表-----------------");
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(posion);
        } catch (Exception e) {

        }
        //
        PageHelper.startPage(pageNo, 4);
        //
        List<ResourceCombination> resourceCombinationList=null;

        System.out.print("是否激活:"+UserUtils.getPrincipal().getUserInfo().getVipId());
        if (UserUtils.getPrincipal().getUserInfo().getVipId()==null){
            model.addAttribute("vipTryFalse","nu");//未激活用户---------
        }else {
            VipInfo vipInfo=new VipInfo();
            vipInfo.setVipId(UserUtils.getPrincipal().getUserInfo().getVipId());
            List<VipInfoDto> listDto = vipInfoService.findAllHaveCardName(vipInfo);
            boolean b=true;
            for (int i=0 ; i< listDto.size() ; i++){
                String CardName = listDto.get(0).getVipCardName();;//待判断的字符串
                String reg=".*学员.*";  //判断字符串中是否含有ll
                b=CardName.matches(reg);
                if (b){
                    break;
                }
            }
            //System.out.print("是否学员："+b);
            //判断加载会员的列表还是学员列表
            if (!b){//判断是会员还是学员
                //获取会员
                //System.out.println("------会员-----");
                try {
                    resourceCombinationList= resourceCombinationService.findAllStrat1Yes(null);
                    if(resourceCombinationList==null){
                        System.out.println("数据库错误");
                    }else {
                        //查看是否激活会员-----是通过用户id关联查找vip表是否存在该用户
                        VipInfo vipCard=new VipInfo();
                        vipCard.setUserId(UserUtils.getPrincipal().getUserInfo().getId());
                        try {
                            VipInfo result_vipvad=vipInfoService.findOne(vipCard);//返回VIP信息结果集
                            if (result_vipvad!=null){
                                model.addAttribute("vipTryFalse","true");
                            }else {
                                model.addAttribute("vipTryFalse","false");
                            }
                            //System.out.println(result_vipvad.getVipId()+"-----------");
                        }catch (Exception n){
                            //System.out.println("------非会员-----");
                        }
                    }
                }catch (Exception n){
                    System.out.println("数据库错误");
                }
            }else {//加载学员的列表
                //获取学员
               // System.out.println("------学员-----");
                try {
                    resourceCombinationList= resourceCombinationService.findAllStrat1No(null);
                    if (resourceCombinationList==null){
                        System.out.println("数据库错误");
                    }else {
                        model.addAttribute("vipTryFalse","true");
                    }
                }catch (Exception n){
                    System.out.println("数据库错误");
                }
            }
        }
        PageInfo page=new PageInfo(resourceCombinationList);
        if (page!=null){
            model.addAttribute("pageInfo",page);
        }
        return "user_study";
    }
}
